<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版本号">
          <a-input placeholder="请输入版本号" v-decorator="['bbBbh', validatorRules.bbBbh ]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上传文件">
          <a-upload :fileList="fileList" :remove="handleRemove" :beforeUpload="beforeUpload">
            <a-button>
              <a-icon type="upload"/>
              选择APK文件
            </a-button>
          </a-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版本更新说明">
          <a-input placeholder="请输入版本更新说明" v-decorator="['bbGxsm', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="版本状态">
          <a-radio-group v-decorator="[ 'bbSfty', validatorRules.bbSfty ]">
            <a-radio :value="0">启用</a-radio>
            <a-radio :value="1">禁用</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {axios} from '@/utils/request'
  import moment from "moment"

  export default {
    name: "AppbbhModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        fileList: [],
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          bbBbh: {rules: [{required: true, message: '请输入版本号!'}]},
          bbSfty: {rules: [{required: true, message: '版本状态'}]},
        },
        url: {
          add: "/appbbh/appbbh/add",
          edit: "/appbbh/appbbh/edit",
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'bbBbh', 'bbGxsm',  'bbSfty'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        if (this.fileList.length === 0) {
          this.$message.error('请选择需要上传的文件!');
          return
        }
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            });
            const fileFormData = new FormData();
            fileFormData.append('file', this.fileList[0]);
            fileFormData.append('bbh', formData.bbBbh);
            this.uploadFileRequest(fileFormData);
          }
        })


      },
      handleCancel() {
        this.close()
      },
      handleRemove(file) {
        console.log(file);
        const index = this.fileList.indexOf(file);
        const newFileList = this.fileList.slice();
        newFileList.splice(index, 1);
        this.fileList = newFileList;
      },
      beforeUpload(file) {
        console.log(file);
        var isAPK = file.type === "application/vnd.android.package-archive";
        var down100MB = file.size / 1024 / 1024 < 100;
        if (!isAPK) {
          this.$message.error('You can only upload APK file!');
        } else if (!down100MB) {
          this.$message.error('APK must smaller than 100MB!');
        } else {
          this.fileList = [];
          this.fileList = [...this.fileList, file];
        }
        return isAPK && down100MB;
      },
      //上传文件
      uploadFileRequest(formData) {
        axios({
          method: 'post',
          url: this.url.fileUpload,
          data: formData,
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        });
      }


    }
  }
</script>

<style lang="less" scoped>

</style>